package neusoft.yuejian_11.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import neusoft.yuejian_11.base.BaseActivity;
import neusoft.yuejian_11.bean.AttentionBean;
import neusoft.yuejian_11.bean.IsAttentionBean;
import neusoft.yuejian_11.bean.TrainBean;
import neusoft.yuejian_11.listener.RetrofitListener;
import neusoft.yuejian_11.model.AttentionModel;
import neusoft.yuejian_11.model.TrainModel;
import neusoft.yuejian_11.R;

public class TrainDetailActivity extends BaseActivity implements RetrofitListener {
    protected Context context;
    private int clubId;
    private int trainId;
    private String trainName;
    private String trainPhon;
    private String trainPicture;
    private TextView textName;
    private ImageButton imageButtonBack;
    private ImageView imageView;
    private TextView textInfo;
    private TextView textAddress;
    private TextView textPrice;
    private TextView textTime;
    private Button buttonPhone;
    private Button buttonTrainn;
    private ImageButton imageButtonFavor;
    private Boolean flags;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_train_detail);

        initViews();//初始化
        initEvents();//点击事件处理
        showData();//传参
        initData();//网络获取
    }

    private void initViews() {
        textName = (TextView) findViewById(R.id.textView_train_name);
        imageView = (ImageView) findViewById(R.id.imageView);
        buttonPhone = (Button) findViewById(R.id.button_phone);

        textInfo = (TextView) findViewById(R.id.text_train_info);
        textAddress = (TextView) findViewById(R.id.text_train_address);
        textPrice = (TextView) findViewById(R.id.text_train_price);
        textTime = (TextView) findViewById(R.id.text_train_time);
        imageButtonBack = (ImageButton) findViewById(R.id.imageButtom_back);
        imageButtonFavor = (ImageButton) findViewById(R.id.imageButtom_favor);
        buttonTrainn = (Button) findViewById(R.id.button_trainn);
    }

    private void initEvents() {
        buttonTrainn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(TrainDetailActivity.this, TrainnActivity.class);
                intent.putExtra("c_id",clubId);
                intent.putExtra("t_id",trainId);
                startActivity(intent);
            }
        });

        buttonPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" +trainPhon ));
                if (ActivityCompat.checkSelfPermission(TrainDetailActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }
                startActivity(intent);
            }
        });

        imageButtonBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        imageButtonFavor.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TrainModel trainModel = new TrainModel();
                trainModel.favorTrain(getU_id(),String.valueOf(trainId),TrainDetailActivity.this);
            }
        });
    }

    private void showData() {
        clubId = getIntent().getIntExtra("c_id",-1);
        trainId = getIntent().getIntExtra("t_id", -1);
        trainName = getIntent().getStringExtra("t_name");
        textName.setText(trainName);
        trainPicture = getIntent().getStringExtra("t_picture");
        Picasso.with(context)
                .load(trainPicture)
                .into(imageView);
        trainPhon = getIntent().getStringExtra("t_phone");
        buttonPhone.setText(trainPhon);
    }

    private void initData() {
        TrainModel trainModel = new TrainModel();
        trainModel.trainDetail(String.valueOf(trainId),this);

        AttentionModel attentionModel = new AttentionModel();
        attentionModel.isCollectedTrain(String.valueOf(getU_id()),String.valueOf(trainId),"0",this);
    }

    @Override
    public void onSuccess(Object o, int flag) {
        if (flag==1) {
            TrainBean trainBeanDetail = (TrainBean) o;
            textInfo.setText(trainBeanDetail.getT_info());
            textAddress.setText(trainBeanDetail.getT_address());
            textPrice.setText(String.valueOf(trainBeanDetail.getT_price()));
            textTime.setText(trainBeanDetail.getT_time());
            buttonPhone.setText(trainBeanDetail.getT_phone());
        }
        if (flag==3){
            IsAttentionBean isAttentionBean = (IsAttentionBean) o;
            if (isAttentionBean.getCollected().equals("1")) {
                //已关注
                flags = true;
                imageButtonFavor.setImageResource(R.drawable.ic_alikepost);
            } else {
                //未关注的
                flags = false;
                imageButtonFavor.setImageResource(R.drawable.ic_likepost);
            }
        }
        if (flag==2) {
            AttentionBean attentionBean=(AttentionBean) o;
            if (attentionBean.getSuccess().equals("1")) {
                Log.e(attentionBean.getSuccess(), "---------");
                if (flags) {
                    flags = false;
                    imageButtonFavor.setImageResource(R.drawable.ic_likepost);
                } else {
                    flags = true;
                    imageButtonFavor.setImageResource(R.drawable.ic_alikepost);

                }
                Toast.makeText(TrainDetailActivity.this, "操作成功", Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    public void onFail() {

    }
}
